<?php

class Admin_UserController extends Zend_Controller_Action
{
	
	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}

    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->USERNAME;
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    	$this->_model = new Default_Model_DbTable_User();
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Người Dùng","PREPEND");
        $this->view->title = "Người Dùng";
        $allUser = $this->_model->GetAll();
        $paginator = Zend_Paginator::factory($allUser);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allUser = $paginator;
    }
    
    public function addAction(){
    	$this->view->headTitle("Thêm Mới","PREPEND");
    	$this->view->title = "Thêm Mới";
    	if($this->getRequest()->isPost()){
    		$dataUser = $this->_request->getPost();
    		if($dataUser){
    			$u = $this->_request->getParam("username");
    			$p = (md5("CTV".md5($this->_request->getParam("password"))."ITTeam"));
    			$e = $this->_request->getParam("email");
    			$g = $this->_request->getParam("gender");
    			$dob = date("Y-m-d",strtotime($this->_request->getParam("DOB")));
    			$a = $this->_request->getParam("address");
    			$active = $this->_request->getParam("active");
    			if($this->_model->GetByName($u)){
    				$this->view->trungten = "Tên đăng nhập đã được sử dụng";
    			}
    			else{
    				$this->_model->AddUser($u, $p, $e, $g, $dob, $a, $active);
    				$this->_redirect("/admin/user");
    			}
    		}
    	}
    }
    
    public function editAction(){
    	$this->view->headTitle("Thêm Mới","PREPEND");
    	$this->view->title = "Sửa";
    	$uid = $this->_getParam("id",0);
    	$user = $this->_model->GetByID($uid);
    	$this->view->user = $user;
    	if($this->getRequest()->isPost()){
    		$dataUser = $this->_request->getPost();
    		if($dataUser){
    			$u = $this->_request->getParam("username");
    			$p = (md5("CTV".md5($this->_request->getParam("password"))."ITTeam"));
    			$e = $this->_request->getParam("email");
    			$g = $this->_request->getParam("gender");
    			$dob = date("Y-m-d",strtotime($this->_request->getParam("DOB")));
    			$a = $this->_request->getParam("address");
    			$active = $this->_request->getParam("active");
    			$this->_model->UpdateUser($uid, $u, $p, $e, $g, $dob, $a, $active);
    			$this->_redirect("/admin/user");
    		}
    	}
    }
    
    public function delAction(){
    	$uid = $this->_getParam("id",0);
    	$this->_model->DelUser($uid);
    	$this->_redirect("/admin/user");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id",0);
    	$this->_model->Active($id);
    	$this->_redirect("/admin/user");
    }


}

